package com.smt.take.ui.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mob.wrappers.PaySDKWrapper
import com.smt.take.R
import com.smt.take.model.beans.OrderBean
import com.smt.take.ui.activity.OrderDetailActivity
import com.smt.take.utils.OrderObservable
import org.jetbrains.anko.find
import org.json.JSONObject
import java.util.*

/**
 * 订单观察者
 */
class OrderRvAdapter(val context: Context?) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),
    Observer {

    init {
        // 让观察者和被观察者绑定关系
        OrderObservable.instance.addObserver(this)
    }

    override fun update(observer: Observable?, data: Any?) {
        // 观察者的响应
        Log.e("order", "观察者收到了消息，消息内容：$data")
        val jsonObject = JSONObject(data as String)
        val pushOrderId = jsonObject.getString("orderId")
        val pushOrderType = jsonObject.getString("type")
        var index = -1
        // 更新UI
        for (i in 0 until orderList.size) {
            val order = orderList[i]
            if (order.id.equals(pushOrderId)) {
                order.type = pushOrderType
                index = i
            }
        }

        if (index != -1) {
            notifyItemChanged(index)
        }
//        notifyDataSetChanged()
    }

    private var orderList: MutableList<OrderBean> = mutableListOf()

    fun setData(orderList: MutableList<OrderBean>) {
        this.orderList = orderList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_order_item, parent, false)
        return OrderViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as OrderViewHolder).bindData(orderList[position])
    }

    override fun getItemCount(): Int {
        return orderList.size
    }

    inner class OrderViewHolder(item: View) : RecyclerView.ViewHolder(item) {

        var orderName: TextView = item.find(R.id.tv_order_item_seller_name)
        var orderType: TextView = item.find(R.id.tv_order_item_type)
        lateinit var orderBean: OrderBean

        init {
            item.setOnClickListener {
                val intent = Intent(context, OrderDetailActivity::class.java)
                intent.putExtra("orderId", orderBean.id)
                intent.putExtra("type", orderBean.type)
                context?.startActivity(intent)
            }
        }

        fun bindData(orderBean: OrderBean) {
            this.orderBean = orderBean
            orderName.text = orderBean.seller?.name
            orderType.text = getOrderTypeInfo(orderBean.type)
        }

    }

    private fun getOrderTypeInfo(type: String?): String {
        /**
         * 订单状态
         * 1 未支付 2 已提交订单 3 商家接单  4 配送中,等待送达 5已送达 6 取消的订单
         */
        //            public static final String ORDERTYPE_UNPAYMENT = "10";
        //            public static final String ORDERTYPE_SUBMIT = "20";
        //            public static final String ORDERTYPE_RECEIVEORDER = "30";
        //            public static final String ORDERTYPE_DISTRIBUTION = "40";
        //            public static final String ORDERTYPE_SERVED = "50";
        //            public static final String ORDERTYPE_CANCELLEDORDER = "60";

        var typeInfo = ""
        when (type) {
            OrderObservable.ORDERTYPE_UNPAYMENT -> typeInfo = "未支付"
            OrderObservable.ORDERTYPE_SUBMIT -> typeInfo = "已提交订单"
            OrderObservable.ORDERTYPE_RECEIVEORDER -> typeInfo = "商家接单"
            OrderObservable.ORDERTYPE_DISTRIBUTION -> typeInfo = "配送中"
            OrderObservable.ORDERTYPE_SERVED -> typeInfo = "已送达"
            OrderObservable.ORDERTYPE_CANCELLEDORDER -> typeInfo = "取消的订单"
        }
        return typeInfo
    }
}